package com.pshcp.patient.api;

import com.external.service.WsRequestService;
import com.external.ws.soap.entity.res.Invoice;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.domain.model.Patient;
import com.pshcp.patient.domain.searchinvoice.ao.InvoiceListAO;
import com.pshcp.patient.domain.searchinvoice.vo.InvoiceListVO;
import com.pshcp.patient.tool.SearchInvoiceTools;
import com.pshcp.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"患者端-查询-电子发票"})
@Slf4j
@RestController
@RequestMapping("/api/patient/searchInvoice")
public class SearchInvoiceAPI {

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private PatientService patientService;

    /**
     * 使用测试数据
     */
    @Value("${ws.test:false}")
    private Boolean hisTest;

    @ApiOperation("发票记录")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "发票记录")
    @PostMapping("/invoiceList")
    public ResultDTO<List<InvoiceListVO>> invoiceList(@RequestBody InvoiceListAO listAO) throws BizException {
        Patient patient = patientService.findById(listAO.getPatientId());
        CommonTool.checkObj(patient, "10120002");
        List<Invoice> invoiceList = wsRequestService.einvoiceList(SearchInvoiceTools.buildWsEinvoiceListDTO(listAO,patient));
        List<InvoiceListVO> voList = CommonTool.copyPropertiesList(invoiceList, InvoiceListVO.class);
        if(hisTest){
            voList = SearchInvoiceTools.buildTestData();
        }
        if(listAO.getBizType() != null){
            voList = voList.stream().filter(v->listAO.getBizType().equals(v.getBizType())).collect(Collectors.toList());
        }
        if(listAO.getState() != null){
            voList = voList.stream().filter(v-> listAO.getState().equals(v.getState())).collect(Collectors.toList());
        }
        return ResultBuilder.success(voList);
    }

}
